﻿using DAL.conn;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
 public    class BasePurchaseBillDAL
    {
        private SqlCommand cmd;
        public BasePurchaseBillDAL()
        {
            cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
        }
        public List <string > FindPurchaseBillIDByPurchaseBillID(string pid)
        {
            cmd.CommandText = "FindPurchaseBillIDByPurchaseBillID";
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@PurchaseID",SqlDbType.NChar ,12).Value =pid ;
            List<string> list = new List<string>();
            SqlDataReader read = SQLHelper.ExcuteReader(cmd);
            while (read .Read ())
            {
                string id = read["PurchaseID"].ToString();
                list.Add(id);
            }
            read.Close();
            return list;
        }
        public List<string> FindPurchaseBillIDByClerk(string cle)
        {
            cmd.CommandText = "FindPurchaseBillIDByClerk";
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@Clerk", SqlDbType.NVarChar, 6).Value = cle ;
            List<string> list = new List<string>();
            SqlDataReader read = SQLHelper.ExcuteReader(cmd);
            while (read.Read())
            {
                string id = read["PurchaseID"].ToString();
                list.Add(id);
            }
            read.Close();
            return list;
        }
        /// <summary>
        /// 根据审核员查询
        /// </summary>
        public List<string> FindPurchaseBillIDByExaminer(string examiner)
        {
            cmd.CommandText = "FindPurchaseBillIDByExaminer";            //存储过程名
            cmd.Parameters.Clear();                                            //清空存储过程参数列表
            cmd.Parameters.Add("@Examiner", SqlDbType.NVarChar, 6).Value = examiner;//添加存储过程参数
            List<string> list = new List<string>();                            //新建string泛型集合用于存储DB读出的订单集合
            SqlDataReader read = SQLHelper.ExcuteReader(cmd);                  //读库
            while (read.Read())
            {
                string id = read["PurchaseID"].ToString();//将读出的记录赋给变量id
                list.Add(id);                             //将id值添加到泛型集合中
            }
            read.Close();
            return list;
        }
        public List<string> FindPurchaseBillIDByDate(DateTime  dat ,DateTime end)
        {
            cmd.CommandText = "FindPurchaseBillIDByDate";
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@startdate", SqlDbType.DateTime ).Value = dat;
            cmd.Parameters.Add("@endDate", SqlDbType.DateTime).Value = end;
            List<string> list = new List<string>();
            SqlDataReader read = SQLHelper.ExcuteReader(cmd);
            while (read.Read())
            {
                string id = read["PurchaseID"].ToString();
                list.Add(id);
            }
            read.Close();
            return list;
        }
        public List<string> FindPurchaseBillIDByOnprocess(int  opn)
        {
            cmd.CommandText = "FindPurchaseBillIDByOnprocess";
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@Onprocess", SqlDbType.SmallInt ).Value = opn ;
           
            List<string> list = new List<string>();
            SqlDataReader read = SQLHelper.ExcuteReader(cmd);
            while (read.Read())
            {
                string id = read["PurchaseID"].ToString();
                list.Add(id);
            }
            read.Close();
            return list;
        }
        public PurchaseBill FindPurchaseBillByPurchaseBillID(string pid ,List<PurchaseDetail> listDetail)
        {
            cmd.CommandText = "FindPruchaseBillByPurchaseID";
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@PurchaseID", SqlDbType.NChar, 12).Value = pid;
            List<PurchaseBill> list = new List<PurchaseBill>();
            SqlDataReader read = SQLHelper.ExcuteReader(cmd);
           if (read.Read())
            {
                PurchaseBill bo = new PurchaseBill();
                bo.PurchaseBillID = read["PurchaseID"].ToString().Trim();
                bo.SupplierID = read["SupplierID"].ToString().Trim();
                bo.PurchaseDate = Convert.ToDateTime(read["PurchaseDate"]);

                if (read["StockDate"] != Convert.DBNull)//判断入库日期是否为空
                    bo.StockDate = Convert.ToDateTime(read["StockDate"]);

                bo.Clerk = read["Clerk"].ToString().Trim();
                //判断审核员Examiner、仓库管理员Custodian是否为空
                if (read["Examiner"] != Convert.DBNull || read["Custodian"] != Convert.DBNull)
                {
                    bo.Examiner = read["Examiner"].ToString().Trim();
                    bo.Custodian = read["Custodian"].ToString().Trim();
                }
                bo.OnProcess = int.Parse(read["OnProcess"].ToString());
                bo.Memo = read["Memo"].ToString();

                bo.PurchaseDetailList = listDetail;
                read.Close();
                return bo;
            }
               else
            {
                read.Close();
                return null;
            }
        }
           
        
        public PurchaseBill FindDetailPurchaseBillByPurchaseBillID(string pid)
        {
            cmd.CommandText = "FindPurchaseBillByPurchaseID";
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@PurchaseID", SqlDbType.NChar, 12).Value = pid;
            
            SqlDataReader read = SQLHelper.ExcuteReader(cmd);

            List<PurchaseDetail> list = new List<PurchaseDetail>();
            while (read.Read())
            {
                PurchaseDetail detail = new PurchaseDetail();
                detail.PurchaseID = read["PurchaseID"].ToString().Trim();
                detail.PurchaseDetailID = Convert.ToInt32(read["PurchaseDatailID"]);
                detail.ProductID = read["ProductID"].ToString().Trim();
                detail.PurchasePrice = Convert.ToDecimal(read["PurchasePrice"]);
                detail.Quantity = int.Parse(read["Quantity"].ToString());

                list.Add(detail);

               
            }
            read.Close();
            return FindPurchaseBillByPurchaseBillID(pid, list);
        }
    }
}
